/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.Arkham;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import android.graphics.Bitmap;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.util.Log;

public class Actor extends Square
{
	private int frameIndex = 0;//current animation index
	public int numFrames;//number of frames for the current acction
	private final static String TAG = Actor.class.getSimpleName();
	float[] mTextureCoords;
	
	public Actor()
	{
		super(1,1);//default square with 1x1 square
	}
	
	public Actor(float width, float height)
	{
		super(width, height);//default to square
		
	}
	
	//
	public Actor(float width, float height, Bitmap inMap, int tileX, int tileY)
	{
		xSize = tileX;//width in pixels of frame
		ySize = tileY;//height in pixels of frame
		
		xDim = width;
		yDim = height;
		float halfWidth = width * 0.5f;
		float halfHeight = height * 0.5f;
		
		//define the vertices of the board
		 float[] Vertices = new float[]
		{
				
				-halfWidth, -halfHeight, 0.0f, //0, bottom left
				 halfWidth, -halfHeight, 0.0f, //1, top left
				-halfWidth,  halfHeight, 0.0f, //2, bottom right
				 halfWidth,  halfHeight, 0.0f, //3, top right
		};
		
		 //draw order of indices
		 short[] indices =  new short[] { 0 , 1, 2,1,3,2};
		
		 //Texture coordinate declaration
		 mTextureCoords = new float[] { 0.0f, 1.0f,
								                1.0f, 1.0f,
									            0.0f, 0.0f,
									            1.0f, 0.0f };
		
		 this.setIndices(indices);//build index buffers
		 this.setVertices(Vertices);//load
		 this.loadBitmap(inMap); // set bitmap
		 
		 //scale the texture coordinates so that only one frame shows at a time
		 
		 this.setTexCoords(updateTextureCoords(mTextureCoords));
		 
		modelView = new float[16];
	}
	
	protected void OnDraw(GL10 gl)
	{
	
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		((GL11)gl).glGetFloatv(GL11.GL_MODELVIEW_MATRIX, modelView, 0);//get the current model view matrix
		gl.glFrontFace(GL10.GL_CCW);
		//change color overlay if it clicked
		if(isClicked)
		{
			mRGBA[0] = 1.0f; mRGBA[1]= 0.25f; mRGBA[2]= 0.25f; mRGBA[3]= 0.75f;
		}
		else
		{
			mRGBA[0] = 1.0f; mRGBA[1] = 1.0f;; mRGBA[2]= 1.0f; mRGBA[3] = 1.0f;
		}
		gl.glColor4f(mRGBA[0], mRGBA[1], mRGBA[2], mRGBA[3]);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		
		
		//load associated texture
		if (mShouldLoadTexture)
		{
			loadGLTexture(gl);
			
			mShouldLoadTexture = false;			
		}
		
		if(mTexID[0] != -1 && mTextureBuffer != null)
		{
			
			if(shouldUpdateTexCord)
			{
				gl.glMatrixMode(GL10.GL_TEXTURE);
				gl.glLoadIdentity();
//				gl.glPushMatrix();
				gl.glTranslatef(xOffset/(mBitmap.getWidth()/(float)xSize), 0.0f, 0.0f);
//				gl.glPopMatrix();
				
				shouldUpdateTexCord = false;
				
			}
			
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexID[0]);
			
			
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
			
			
		}
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		gl.glTranslatef(mTranslate[0], mTranslate[1], mTranslate[2]);
		gl.glRotatef(mRotation[0], 1, 0, 0 );
		gl.glRotatef(mRotation[1], 0, 1, 0 );
		gl.glRotatef(mRotation[2], 0, 0, 1 );
		
		//gl.glScalef(mScale[0],mScale[1], mScale[2]);
		
		gl.glDrawElements(GL10.GL_TRIANGLES, mNumIndices,
						  GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
		gl.glPopMatrix();
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
		
		if(mTexID[0] != -1 && mTextureBuffer != null)
		{
			gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glDisable(GL10.GL_TEXTURE_2D);
		}
		

	

		gl.glFinish();
		
	}
	
	protected void onUpdate(GL10 gl, int tickCount)
	{
		
		if(tickCount % 5 == 0)
		{
			//Log.i(TAG," in actor update X Offset " + xOffset);
			if(xOffset < numXtile-1)
				xOffset++ ;
			else
				
			    xOffset = 0;
		
		//mRotation[2]+= 2.0;
		
	
		
		//mTextureCoords = TranslateTexture(gl, xOffset, yOffset,this.mTextureCoords );
		//setTexCoords(mTextureCoords);
		shouldUpdateTexCord = true;
		}
	}
	
	protected void loadGLTexture(GL10 gl)
	{
		mSpriteID = new int[1];
		// Generate one texture pointer...
		int[] textures = new int[1];
		gl.glGenTextures(textures.length, textures, 0);
		mTexID[0] = textures[0];
		
		
		int err = gl.glGetError();
		if(err != GL10.GL_NO_ERROR)
			Log.e(TAG, " GL GEN tex error: " + GLU.gluErrorString(err));
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexID[0]);
		
		 err = gl.glGetError();
		if(err != GL10.GL_NO_ERROR)
			Log.e(TAG, " GL Bind tex error: " + GLU.gluErrorString(err));
		
		assert err == GL10.GL_NO_ERROR;

		// Create Nearest Filtered Texture
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		

	
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
		//gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
			err = gl.glGetError();
			if(err != GL10.GL_NO_ERROR)
				Log.e(TAG, " glTexParameterf error: " + GLU.gluErrorString(err));
			
			assert err == GL10.GL_NO_ERROR;

//			gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA,mBitmap.getWidth(), mBitmap.getHeight(), 0,GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, mPixelColor);
//			err = gl.glGetError();
//			if(err != GL10.GL_NO_ERROR)
//				Log.e(TAG, " glTexParameterf error: " + GLU.gluErrorString(err));
			
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
	
			
		assert err == GL10.GL_NO_ERROR;
		//mBitmap.recycle();
		
		gl.glMatrixMode(GL10.GL_TEXTURE);
		gl.glLoadIdentity();
		gl.glPushMatrix();
		gl.glScalef(1.0f/(mBitmap.getWidth()/(float)xSize), 1.0f/(mBitmap.getHeight()/ (float)ySize ), 1.0f);
		gl.glPopMatrix();
		
		
		
	}

	
	
	

}
